<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\SpeakerResumeLogic;
use app\cms\model\InvitationApply;
use app\cms\model\Project;
use app\cms\model\Speaker;
use app\cms\model\Users;
use app\cms\validate\SpeakerResumeValidate;
use app\model\DownloadZip;
use plugin\saiadmin\app\logic\system\SystemConfigLogic;
use plugin\saiadmin\app\logic\system\SystemMenuLogic;
use plugin\saiadmin\app\model\system\SystemMenu;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use support\Db;
use support\Request;
use support\Response;
use think\facade\Db as FacadeDb;
use Tinywan\Storage\Storage;
use plugin\saiadmin\utils\Arr;
use plugin\saiadmin\utils\Helper;
use support\Log;

/**
 * 首页仪表盘
 */
class HomeController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        // 外讲数量
        $speakerCount = Speaker::where(['status' => 1, 'is_display' => 1])->whereNull('delete_time')->count();
        // 邀约申请数量
        $invitationCount = InvitationApply::whereNull('delete_time')->count();
        // 项目数量
        $projectCount = Project::where(['is_display' => 1])->whereNull('delete_time')->count();
        // 项目完结数量
        $projectEndCount = Project::where(['is_display' => 1, 'project_status' => 2])->whereNull('delete_time')->count();
        // 平台用户数量
        $userCount = Users::where('status', 1)->whereNull('delete_time')->count();
        // 进行中的项目 [['id'=>'','name'=>'','order_sn'=>''],[],[]···]
        $projectHaveCount = Project::where(['is_display' => 1, 'project_status' => 1])->whereNull('delete_time')->field('id,project_name,project_code,order_sn')->order('id', 'desc')->limit(10)->select();
        // 邀约讲者排行榜 array
        $invitationSpeakerCount = Speaker::where(['status' => 1, 'is_display' => 1])->field('id,name,name_en,title,photo,number_of_invitations')->order('number_of_invitations', 'desc')->whereNull('delete_time')->limit(10)->select();

        $data = [
            'speakerCount' => $speakerCount,
            'invitationCount' => $invitationCount,
            'projectCount' => $projectCount,
            'projectEndCount' => $projectEndCount,
            'userCount' => $userCount,
            'projectHaveCount' => $projectHaveCount,
            'invitationSpeakerCount' => $invitationSpeakerCount,
        ];
        return $this->success($data);
    }

    /**
     * 首页-外讲库录入年度统计
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-10-10
     * @param Request $request
     * @return Response
     */
    public function speaker(Request $request): Response
    {
        $where = $request->more([
            ['speaker_datetime', ''],
        ]);

        $year = isset($where['speaker_datetime']) && !empty($where['speaker_datetime']) ? $where['speaker_datetime'] : date('Y');

        $list = FacadeDb::query('SELECT MONTH(create_time) AS month, COUNT(*) AS speaker_count FROM eb_speaker where YEAR(create_time) = ?  GROUP BY MONTH', [$year]);
        // var_dump($list);
        // 日期为空就查询当前年的12个月的数量
        $month = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0];
        // var_dump($month);

        foreach ($list as $k => $v) {
            foreach ($month as $key => $val) {
                if ($v['month'] == $key) {
                    $month[$v['month']] = $v['speaker_count'];
                }
            }
        }

        return $this->success(array_values($month));
    }

    /**
     * 首页-项目数量年度统计
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-10-10
     * @param Request $request
     * @return Response
     */
    public function project(Request $request): Response
    {
        $where = $request->more([
            ['project_datetime', ''],
        ]);

        $year = isset($where['project_datetime']) && !empty($where['project_datetime']) ? $where['project_datetime'] : date('Y');

        $list = FacadeDb::query('SELECT MONTH(create_time) AS month, COUNT(*) AS project_count FROM eb_project where YEAR(create_time) = ?  GROUP BY MONTH', [$year]);
        // var_dump($list);
        // 日期为空就查询当前年的12个月的数量
        $month = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0];
        // var_dump($month);

        foreach ($list as $k => $v) {
            foreach ($month as $key => $val) {
                if ($v['month'] == $key) {
                    $month[$v['month']] = $v['project_count'];
                }
            }
        }

        return $this->success(array_values($month));
    }


    /**
     * 首页-会员数量年度统计
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-10-10
     * @param Request $request
     * @return Response
     */
    public function user(Request $request): Response
    {
        $where = $request->more([
            ['user_datetime', ''],
        ]);
        $year = isset($where['user_datetime']) && !empty($where['user_datetime']) ? $where['user_datetime'] : date('Y');

        $list = FacadeDb::query('SELECT MONTH(create_time) AS month, COUNT(*) AS user_count FROM eb_users where YEAR(create_time) = ?  GROUP BY MONTH', [$year]);
        // var_dump($list);
        // 日期为空就查询当前年的12个月的数量
        $month = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0];
        // var_dump($month);

        foreach ($list as $k => $v) {
            foreach ($month as $key => $val) {
                if ($v['month'] == $key) {
                    $month[$v['month']] = $v['user_count'];
                }
            }
        }

        return $this->success(array_values($month));
    }


    /**
     * 查看消息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-18
     * @param Request $request
     * @return void
     */
    public function see(Request $request)
    {
        $where = $request->more([
            ['id', ''],
            ['created_by', ''],
        ]);

        $result = DownloadZip::where($where)->update([
            'read_status' => 1,
        ]);
        return $this->success();
    }

    /**
     * 获取七牛云token
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-18
     * @return void
     */
    public function getQiniuToken()
    {
        $uploadMrg = new UploadManager();
        $auth = new Auth(config('app.qiniu.accessKey'), config('app.qiniu.secretKey'));
        $token = $auth->uploadToken(config('app.qiniu.bucket'));

        $logic = new SystemConfigLogic();
        $uploadConfig = $logic->getGroup('upload_config');
        $allow_file = Arr::getConfigValue($uploadConfig, 'upload_allow_file');
        $allow_image = Arr::getConfigValue($uploadConfig, 'upload_allow_image');
        return $this->success(['token' => $token, 'allow_file' => $allow_file, 'allow_image' => $allow_image]);
    }


    public function check()
    {
        $menu = SystemMenu::select();
        $arr = Helper::makeTree($menu->toArray());
        Log::info(' 菜单 ' . json_encode($arr[8]));
    }
}
